/**  
* @Title: BannerController.java  
* @Description: TODO(用一句话描述该文件做什么)  
* @author 张鑫
* @phone 17561783855  
* @date 2019年8月8日  
*/
package com.fortune.rainshine.system.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.fortune.rainshine.common.BaseController;
import com.fortune.rainshine.common.service.AliOssService;
import com.fortune.rainshine.common.utils.StringUtil;
import com.fortune.rainshine.system.service.BannerService;
import com.fortune.rainshinecommon.base.AliOssParmsModel;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.AliOssTypeEnum;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.system.BannerModel;
import com.fortune.rainshinecommon.model.system.CardModel;
import com.fortune.rainshinecommon.model.system.User;
import com.fortune.rainshinecommon.util.OssFileNameUtils;
import com.fortune.rainshinecommon.util.PicUtils;

/**  
* @ClassName: BannerController  
* @Description: TODO(官网banner图维护)  
* @author 张鑫
* @phone 17561783855
* @date 2019年8月8日  
*    
*/
@Controller
@RequestMapping("/system/banner")
public class BannerController extends BaseController {
	private final static Logger logger = LoggerFactory.getLogger(BannerController.class);
	
	@Autowired
	private AliOssService aliOssService;
	@Autowired
	private BannerService bannerService;
	
	@RequiresPermissions("banner:view")
	@RequestMapping()
	public String card(Model model) {
		return "banner/bannerList.html";
	}
	
	@RequestMapping("/editForm")
	public String addUser(Model model) {
		return "banner/banner_form.html";
	}
	
	@RequiresPermissions("banner:add")
	@ResponseBody
	@RequestMapping("/add")
	public MsgModel add(@RequestBody BannerModel model) {
		try {
			User user = this.getLoginUser();
			model.setUser(user);
			model.setCreateId(user.getUserId());
			model.setUpdateId(user.getUserId());
			return bannerService.add(model);
		} catch (Exception e) {
			logger.error("add ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}
	}
	
    @RequiresPermissions("banner:edit")
    @ResponseBody
    @RequestMapping("/update")
    public MsgModel update(@RequestBody BannerModel model) {
    	try {
    		model.setCreateId(this.getLoginUser().getUserId());
            return bannerService.update(model);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			return new MsgModel(ResultCode.FAILURE);
		}
    	
    }
	
    @RequiresPermissions("banner:view")
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public MsgModel list(Integer page, Integer limit, String searchKey, String searchValue) {
    	try {
    		if (page == null) {
                page = 0;
                limit = 0;
            }
            if (StringUtil.isBlank(searchValue)) {
                searchKey = null;
            }
            ParmsModel parm = new ParmsModel(page,limit,searchKey,searchValue);
            MsgModel ms = bannerService.list(parm);
            return ms;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			return new MsgModel(ResultCode.FAILURE);
		}
    }
    
    @RequiresPermissions("banner:delete")
    @ResponseBody
    @RequestMapping("/delete")
    public MsgModel delete(@RequestBody BannerModel model) {
    	try {
    		return bannerService.delete(model);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			return new MsgModel(ResultCode.FAILURE);
		}
        
    }
	
	
	@RequiresPermissions("banner:upload")
	@ResponseBody
	@RequestMapping("/upload")
	public MsgModel upload(@RequestBody MultipartFile file) throws IOException {
		if(file == null){
			return new MsgModel("请选择文件", ResultCode.FAILURE);
		}
		String originalFileName = file.getOriginalFilename();
		if(originalFileName == null || !originalFileName.contains(".")){
			return new MsgModel("文件格式不正确", ResultCode.FAILURE);
		}
		String prefix = originalFileName.substring(originalFileName.lastIndexOf('.')).toLowerCase();
		String[] picTypes = {".jpg",".bmp",".png",".jpeg"};
		if (!Arrays.asList(picTypes).contains(prefix)) {
			return new MsgModel("请上传jpg/bmp/png/jpeg格式的图片!", ResultCode.FAILURE);
		}
		if(file.getSize()/10240 > (double) 3000){
			return new MsgModel("上传图片不能大于2.5M!", ResultCode.FAILURE);
		}
		try {
			String floder = AliOssTypeEnum.getValue(3);
			String rannum = UUID.randomUUID().toString().replaceAll("-", "");
			String fileName = OssFileNameUtils.getNewName(file, floder, rannum);
			byte[] bytes = PicUtils.quality(file.getBytes(), 1f);
			AliOssParmsModel model = new AliOssParmsModel(rannum, floder, fileName, bytes);
			aliOssService.uploadFile(model);
			String path = OssFileNameUtils.getFilePath(floder, fileName);
			String filepath = aliOssService.getUrl(path);
			return new MsgModel(ResultCode.SUCCESS, filepath);
		} catch (Exception e) {
			logger.error("upload ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}
	}

}
